from leetcode_test import TreeNode

class Solution:
    def pseudoPalindromicPaths (self, root: TreeNode) -> int:
        count = 0
        def helper(root, l):
            nonlocal count
            if not root.left and not root.right:
                print("l=", l)
                n = len(l)
                if root.val in l:
                    n -= 1
                else:
                    n += 1
                if n <= 1:
                    count += 1
                return
            print(l)
            if root.val in l:
                t = l.copy()
                t.remove(root.val)
                if root.left:
                    helper(root.left, t)
                if root.right:
                    helper(root.right, t)
            else:
                t = l.copy()
                t.append(root.val)
                if root.left:
                    helper(root.left, t)
                if root.right:
                    helper(root.right, t)

        helper(root, [])
        return count


def test():
    root = TreeNode.create( [2,1,1,1,3,None,None,None,None,None,1])
    assert Solution().pseudoPalindromicPaths(root) == 1
    
    root = TreeNode.create( [2,3,1,3,1,None, 1])
    assert Solution().pseudoPalindromicPaths(root) == 2

